package com.my.xinyidaixi.Adapter;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.my.xinyidaixi.Activity.jianxi;
import com.my.xinyidaixi.Model.GoodsItem;
import com.my.xinyidaixi.R;

/**
 * Created by zw on 2017/7/27.
 */
public class bottomselectgoodAdapter extends RecyclerView.Adapter<bottomselectgoodAdapter.ViewHolder> {

    private SparseArray<GoodsItem> datalist;
   // private DecimalFormat df;
    private LayoutInflater mInflater;
  /*  private Goods_interface goods_interface;

    public void huidiao_Goods_interface(Goods_interface goodsinterface)
    {
        this.goods_interface=goodsinterface;
    }*/

    private jianxi jianxi;
    public bottomselectgoodAdapter(jianxi context, SparseArray<GoodsItem> datalist) {
        jianxi=context;
        this.datalist = datalist;
        //df = new DecimalFormat("#.00");
        mInflater=LayoutInflater.from(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view=mInflater.inflate(R.layout.bottomselectgoods,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position)
    {
        GoodsItem item= datalist.valueAt(position);
        holder.binData(item);
    }

    @Override
    public int getItemCount()
    {
        if(datalist==null)
        {
            return 0;
        }
        return datalist.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        private GoodsItem item;
        private TextView Count,tvName,tvCost,tvMinus,tvAdd;
        public ViewHolder(View itemView)
        {
            super(itemView);
            Count = (TextView) itemView.findViewById(R.id.Count);
            tvName = (TextView) itemView.findViewById(R.id.tvName);
            tvCost = (TextView) itemView.findViewById(R.id.tvCost);
            tvMinus = (TextView) itemView.findViewById(R.id.tvMinus);
            tvAdd = (TextView) itemView.findViewById(R.id.tvAdd);

            tvMinus.setOnClickListener(ViewHolder.this);

            tvAdd.setOnClickListener(ViewHolder.this);

        }

        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.tvAdd:
                    jianxi.add(item);
                    break;
                case R.id.tvMinus:
                   jianxi.remove(item);
                    break;
                default:break;
            }
        }

        public void binData(GoodsItem item)
        {
            this.item = item;
            tvName.setText(item.name);
            tvCost.setText(String.valueOf(item.count*item.price));
            Count.setText(String.valueOf(item.count));
        }
    }
}
